// Generate four random hex digits.
const uploadUrl = 'site-statistics'
function ajax(method, url, data, header = {}) {
  return new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest, params = [];
    //  xhr.responseType = 'json';// 指定返回类型
    xhr.onreadystatechange = () => {
      switch (xhr.status) {
        case 200: resolve(xhr.response);
          break;
        case 204: resolve()
          break;
        case 401:// 401登录失败处理
          //do something
          reject();
          break;
        default: reject({ status: xhr.status, res: xhr.response });
      }
    };
    if (data) {// 将参数编码成表单的键值对形式
      // for (let k in data) params.push(`${encodeURIComponent(k)}=${encodeURIComponent(data[k])}`);
      // data = params.join('&');
      data = JSON.stringify(data)
    }
    xhr.open(method, url, true);
    Object.entries(header).forEach(([key, value]) => {
      xhr.setRequestHeader(key, value)
    })
    xhr.setRequestHeader('Content-type', 'application/json');// 设置content-type
    // xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');// 设置content-type
    // xhr.withCredentials = true;// 支持跨域发送cookies
    xhr.send(data);
  });
}
function mapLogList(arr) {
  const result = []
  while (arr.length > 0) {
    const row = arr.pop()
    if (result.find((r) => r.login_name === row.login_name)) {
      const ami = result.find((_r) => (_r.login_name === row.login_name && _r.log_type === row.log_type))
      if (ami) {
        ami.count += 1
      } else {
        result.push(row)
      }
    } else {
      result.push(row)
    }
  }
  return result
}
export const ieUpload = (serverUrl, arr, appId, ip) => {
  const ids = arr.map(r => r.id)
  const _arr = mapLogList(arr)
  const data_arr = _arr.map(r => {
    return {
      appId: appId,
      count: r.count,
      dataType: r.log_type,
      identify: r.login_name
    }
  })
  const header = {
    'Stjh-Real-IP': ip + ''
  }
  ajax('post', serverUrl + '/' + uploadUrl, data_arr, header).then(function (res) {
    if (res.code === 0) {
      // postMessage({ cmd: 'UPLOAD_SUCCESS', ids })
      // ieUpload()
    } else {
    }
  }).catch(function (err) {
  })
}
// function mapLogList(arr) {
//   const result = []
//   while (arr.length > 0) {
//     const row = arr.pop()
//     if (result.find(function (item) {
//       return item.login_name === row.login_name
//     })) {
//       const ami = result.find(function (_r) {
//         return _r.login_name === row.login_name && _r.log_type === row.log_type
//       })
//       if (ami) {
//         ami.count += 1
//       } else {
//         result.push(row)
//       }
//     }
//   }
//   return result
// }
// export function ieUpload(serverUrl, arr, appId, ip) {
//   const ids = []
//   arr.forEach(function (item) {
//     ids.push(item.id)
//   });
//   const _arr = mapLogList(arr)
//   const data_arr = []
//   _arr.forEach(function (r) {
//     data_arr.push({
//       appId: appId,
//       count: r.count,
//       dataType: r.log_type,
//       identify: r.login_name
//     })
//   })
//   const header = {
//     'Stjh-Real-IP': ip + ''
//   }
//   ajax('post', serverUrl + uploadUrl, data_arr, header)
// }